//
//  DiceModel.m
//
//  Created by Hjelm Daniel on 8/3/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DiceModel.h"

#define DICE_SIDE 1

#define CORNER_A  -DICE_SIDE,  DICE_SIDE,  DICE_SIDE
#define CORNER_B   DICE_SIDE,  DICE_SIDE,  DICE_SIDE
#define CORNER_C  -DICE_SIDE, -DICE_SIDE,  DICE_SIDE
#define CORNER_D   DICE_SIDE, -DICE_SIDE,  DICE_SIDE

#define CORNER_E  -DICE_SIDE,  DICE_SIDE, -DICE_SIDE
#define CORNER_F   DICE_SIDE,  DICE_SIDE, -DICE_SIDE
#define CORNER_G  -DICE_SIDE, -DICE_SIDE, -DICE_SIDE
#define CORNER_H   DICE_SIDE, -DICE_SIDE, -DICE_SIDE

#define SIDE_SIZE  0.125

@implementation DiceModel

@synthesize x_rotation;
@synthesize y_rotation;
@synthesize z_rotation;
@synthesize rotspeed;
@synthesize rotstop;
@synthesize touchInside;
@synthesize startTouchPosition;


const GLfloat squareVertices[] = {
	// 1 - side
	CORNER_A,
	CORNER_C,
	CORNER_B,
	CORNER_B,
	CORNER_C,
	CORNER_D,
	
	// 2 - side
	CORNER_A,
	CORNER_B,
	CORNER_E,
	CORNER_E,
	CORNER_B,
	CORNER_F,
	
	// 3 - side
	CORNER_A,
	CORNER_E,
	CORNER_C,
	CORNER_C,
	CORNER_E,
	CORNER_G,
	
	// 4 - side
	CORNER_B,
	CORNER_D,
	CORNER_F,
	CORNER_F,
	CORNER_D,
	CORNER_H,
	
	// 5 - side
	CORNER_C,
	CORNER_G,
	CORNER_D,
	CORNER_D,
	CORNER_G,
	CORNER_H,
	
	// 6 - side
	CORNER_E,
	CORNER_F,
	CORNER_G,
	CORNER_G,
	CORNER_F,
	CORNER_H,
};

const GLfloat squareTexCoords[] = {
	// 1 - side
	0 * SIDE_SIZE, 0 * SIDE_SIZE,
	0 * SIDE_SIZE, 1 * SIDE_SIZE,
	1 * SIDE_SIZE, 0 * SIDE_SIZE,
	1 * SIDE_SIZE, 0 * SIDE_SIZE,
	0 * SIDE_SIZE, 1 * SIDE_SIZE,
	1 * SIDE_SIZE, 1 * SIDE_SIZE,
	
	// 2 - side
	1 * SIDE_SIZE, 0 * SIDE_SIZE,
	1 * SIDE_SIZE, 1 * SIDE_SIZE,
	2 * SIDE_SIZE, 0 * SIDE_SIZE,
	2 * SIDE_SIZE, 0 * SIDE_SIZE,
	1 * SIDE_SIZE, 1 * SIDE_SIZE,
	2 * SIDE_SIZE, 1 * SIDE_SIZE,
	
	// 3 - side
	2 * SIDE_SIZE, 0 * SIDE_SIZE,
	2 * SIDE_SIZE, 1 * SIDE_SIZE,
	3 * SIDE_SIZE, 0 * SIDE_SIZE,
	3 * SIDE_SIZE, 0 * SIDE_SIZE,
	2 * SIDE_SIZE, 1 * SIDE_SIZE,
	3 * SIDE_SIZE, 1 * SIDE_SIZE,
	
	// 4 - side
	3 * SIDE_SIZE, 0 * SIDE_SIZE,
	3 * SIDE_SIZE, 1 * SIDE_SIZE,
	4 * SIDE_SIZE, 0 * SIDE_SIZE,
	4 * SIDE_SIZE, 0 * SIDE_SIZE,
	3 * SIDE_SIZE, 1 * SIDE_SIZE,
	4 * SIDE_SIZE, 1 * SIDE_SIZE,
	
	// 5 - side
	4 * SIDE_SIZE, 0 * SIDE_SIZE,
	4 * SIDE_SIZE, 1 * SIDE_SIZE,
	5 * SIDE_SIZE, 0 * SIDE_SIZE,
	5 * SIDE_SIZE, 0 * SIDE_SIZE,
	4 * SIDE_SIZE, 1 * SIDE_SIZE,
	5 * SIDE_SIZE, 1 * SIDE_SIZE,
	
	// 6 - side
	5 * SIDE_SIZE, 0 * SIDE_SIZE,
	5 * SIDE_SIZE, 1 * SIDE_SIZE,
	6 * SIDE_SIZE, 0 * SIDE_SIZE,
	6 * SIDE_SIZE, 0 * SIDE_SIZE,
	5 * SIDE_SIZE, 1 * SIDE_SIZE,
	6 * SIDE_SIZE, 1 * SIDE_SIZE,	
};

-(void)setTexture: (GLint)newIndex {
	index = newIndex;
}


-(id)init {
	x_rotation = 0.0;
	y_rotation = 0.0;
	z_rotation = 0.0;
	rotspeed = 0.8f;
	index = 0;
	return self;
}

-(void)drawModel {

	if (x_rotation > 360.0f) {
		x_rotation = 0;
	}	
	if (y_rotation > 360.0f) {
		y_rotation = 0;
	}	
	if (z_rotation > 360.0f) {
		z_rotation = 0;
	}
	
	glPushMatrix();
	glTranslatef(0, 0, -4.0f);
	//glRotatef(rotation, 1.0f, 0.9f, 0.2f);
	glRotatef(x_rotation, 1.0f, 0.0f, 0.0f);
	glRotatef(y_rotation, 0.0f, 1.0f, 0.0f);
	glRotatef(z_rotation, 0.0f, 0.0f, 1.0f);
	
	if (rotstop) {
		x_rotation = floor(x_rotation / 90.0f) * 90;
		y_rotation = floor(y_rotation / 90.0f) * 90;
		z_rotation = floor(z_rotation / 90.0f) * 90;
	}
	else {
		x_rotation += rotspeed;
		y_rotation += rotspeed;
		z_rotation += rotspeed;
	}
	
	GLfloat textureArray[72];
	for (int i=0; i < 6; i++) {
		textureArray[i*12+0] = i * SIDE_SIZE;
		textureArray[i*12+2] = i * SIDE_SIZE;
		textureArray[i*12+4] = (i+1) * SIDE_SIZE;
		textureArray[i*12+6] = (i+1) * SIDE_SIZE;
		textureArray[i*12+8] = i * SIDE_SIZE;
		textureArray[i*12+10] = (i+1) * SIDE_SIZE;
		
		textureArray[i*12+1] = index * SIDE_SIZE;
		textureArray[i*12+5] = index * SIDE_SIZE;
		textureArray[i*12+7] = index * SIDE_SIZE;
		textureArray[i*12+3] = (index+1) * SIDE_SIZE;
		textureArray[i*12+9] = (index+1) * SIDE_SIZE;
		textureArray[i*12+11] = (index+1) * SIDE_SIZE;
	}
	
	//NSLog(@"x_rotation = %f", x_rotation);
	glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, textureArray);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnable(GL_TEXTURE_2D);
    glDrawArrays(GL_TRIANGLES, 0, 36);
	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}

@end
